/*
  Name: 
  Copyright: 
  Author: 
  Date: 15/01/08 23:26
  Description: 
*/

#include "DeadProxyPacketLog.h"
#include <wx/arrimpl.cpp> //this is needed for wxArrays, i dont know why lol
WX_DEFINE_OBJARRAY(ArrayOfPackets);

wxString * Packet::GetString()
{
    wxString * ret = new wxString;
    ret->Printf(" %i bytes", length);
    if(sendrecv == 0)
    {
        ret->Prepend(wxT("Send:"));
    } else {
        ret->Prepend(wxT("Recv:"));
    }
    return ret;
}

wxString * Packet::GetPacketString()
{
    wxString * output;
    output = new wxString;
    if(sendrecv == 0)
    {
        output->Printf("send\n");
    } else {
        output->Printf("recv\n");
    }
    int rows;
    if(length%16 == 0)
    {
        rows = length / 16;
    } else {
        rows = (length - length%16 + 16)/16;
    }
    for(int i = 0; i < rows; i++)
    {
        for(int ii = 0; ii < 16; ii++)
        {
            if(i * 16 + ii >= length)
            {
                output->Append(wxT("   "));
            } else {
                if(buffer[i * 16 + ii]<16)
                {
                    output->Append(wxT("0"));
                }
                output->Append(wxString::Format("%X ",buffer[i * 16 + ii]));
            }
        }
        output->Append(wxString::Format("   "));
        for(int ii = 0; ii < 16; ii++)
        {
            if(i * 16 + ii >= length)
            {
                break;
            }
            if(buffer[i * 16 + ii] >= 32 && buffer[i * 16 + ii] <= 126)
            {
                output->Append(wxString::Format("%c",buffer[i * 16 + ii]));
            } else {
                output->Append(wxT("."));
            }
        }
        output->Append(wxT("\n"));
    }
    return output;
}

Packet::Packet(unsigned char * buf, int len, int sr)
{
    buffer = buf;
    length = len;
    sendrecv = sr;
}

Packet::~Packet()
{
    delete[] buffer;
}


wxString * PacketLog::AddPacket(unsigned char * buffer, int len, int sr)
{
    Packet * pack = new Packet(buffer, len, sr);
    mutex->Lock();
    Packets->Add(pack);
    mutex->Unlock();
    return pack->GetString();
}

void PacketLog::ClearLog()
{
    mutex->Lock();
    Packets->Clear();
    mutex->Unlock();
}


wxArrayString * PacketLog::GetListBoxArray()
{
    mutex->Lock();
    int n = Packets->GetCount();
    wxArrayString * Ret = new wxArrayString;
    Ret->Alloc(n);
    for(int i = 0; i < n; i++)
    {
        Ret->Add(*Packets->Item(i).GetString());
    }
    mutex->Unlock();
    return Ret;
}

wxString * PacketLog::GetPacketString(int i)
{
    wxString * Ret;
    mutex->Lock();
    Ret = Packets->Item(i).GetPacketString();
    mutex->Unlock();
    return Ret;
}

PacketLog::PacketLog()
{
    Packets = new ArrayOfPackets;
    mutex = new wxMutex;
}

PacketLog::~PacketLog()
{
    mutex->Lock();
    delete Packets;
    mutex->Unlock();
    delete mutex;
}
        
